﻿Imports System.IO
Imports AjaxControlToolkit

Public Class AddShopOutlets
    Inherits System.Web.UI.Page

    Public dbmanager As DBManager = New DBManager()
    Private Shared businessID As Integer
    Private Shared imageID As Integer

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'disable all the textbox
        tbxBusinessName.Enabled = False
        ddlCategory.Enabled = False
        tbxShopURL.Enabled = False
        tbxShopDetails.Enabled = False
        tbxShopOutletName.Enabled = False
        tbxOutletAddress.Enabled = False
        tbxShopOutletTel.Enabled = False
        tbxOpeningHours.Enabled = False
        tbxRemarks.Enabled = False

        If Request.QueryString("bID") IsNot Nothing Then
            Dim businessID = Request.QueryString("bID")
        End If


    End Sub

    Protected Sub rblBusinessType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rblBusinessType.SelectedIndexChanged
        ' if the user selected a new business , the search box will not appear and the textbox will appear for the user to input 
        If (rblBusinessType.SelectedIndex = 0) Then
            SearchPanel.Visible = False
            SearchResultPanel.Visible = False
            ' the textbox all can type in the business detail panel
            tbxBusinessName.Enabled = True
            ddlCategory.Enabled = True
            tbxShopURL.Enabled = True
            tbxShopDetails.Enabled = True
            'the textbox all can type in the outlet detail panel
            tbxShopOutletName.Enabled = False
            tbxOutletAddress.Enabled = False
            tbxShopOutletTel.Enabled = False
            tbxOpeningHours.Enabled = False
            tbxRemarks.Enabled = False

            ' if not it will appear 
        Else
            SearchPanel.Visible = True
            SearchResultPanel.Visible = True
            ' the textbox all can type in the business detail panel
            tbxBusinessName.Enabled = False
            ddlCategory.Enabled = False
            tbxShopURL.Enabled = False
            tbxShopDetails.Enabled = False
            'the textbox all can type in the business detail panel  ( accordion)
            tbxShopOutletName.Enabled = False
            tbxOutletAddress.Enabled = False
            tbxShopOutletTel.Enabled = False
            tbxOpeningHours.Enabled = False
            tbxRemarks.Enabled = False

        End If

        tbxBusinessName.Text = ""
        ddlCategory.SelectedItem.Text = ""
        tbxShopURL.Text = ""
        tbxShopDetails.Text = ""

        tbxShopOutletName.Text = ""
        tbxOutletAddress.Text = ""
        tbxShopOutletTel.Text = ""
        tbxOpeningHours.Text = ""
        tbxRemarks.Text = ""

        hfBusinessID.Value = 0
        hfImageID.Value = 0
        GridView1.DataBind()
    End Sub

    Protected Sub SearchResult_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles SearchResult.SelectedIndexChanged
        Dim lbname As LinkButton = e.Item.FindControl("lbname")
        Dim hlWebsite As HyperLink = e.Item.FindControl("hlWebsite")
        Dim BusinessDetails As Label = e.Item.FindControl("BusinessDetails")

    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click

    End Sub


    Protected Sub BusinessDone_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BusinessDone.Click
        'Adding the user input in the database
        Dim datecreated As Date
        datecreated = Today.Date
        Dim visits As Integer
        visits = 0
        businessID = dbmanager.addBusinessDetails(tbxBusinessName.Text, ddlCategory.SelectedItem.Text, tbxShopURL.Text, tbxShopDetails.Text, datecreated, visits)
        tbxBusinessName.Enabled = False

        ddlCategory.Enabled = False
        tbxShopURL.Enabled = False
        tbxShopDetails.Enabled = False
        'the textbox all can type in the accordion
        tbxShopOutletName.Enabled = True
        tbxOutletAddress.Enabled = True
        tbxShopOutletTel.Enabled = True
        tbxOpeningHours.Enabled = True
        tbxRemarks.Enabled = True
        If (tbxBusinessName.Text = "") Then
            lblBusinessNameValidator.Visible = True
        End If

        If (ddlCategory.SelectedItem.Text = "") Then
            lblCategoryValidator.Visible = True
        End If
        If (tbxBusinessName.Text = "" And ddlCategory.SelectedItem.Text = "") Then
            lblBusinessNameValidator.Visible = True
            lblCategoryValidator.Visible = True
        End If
        hfBusinessID.Value = businessID
        imageID = Convert.ToInt32(hfImageID.Value)

        dbmanager.assignImageBusiness(imageID, businessID)
    End Sub

    Protected Sub BtnDoneOD_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnDoneOD.Click
        Dim datecreated As Date
        datecreated = Today.Date

        MsgBox("Are you sure you finish adding the outlet ? ", MsgBoxStyle.OkCancel, "Confirmation")
        If (MsgBoxResult.Ok) Then
            '  Adding the user input in the database
            If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
               ) Then
                MsgBox("Successfully added outlet", MsgBoxStyle.OkOnly, "Success")
                'refresh of gridview
                GridView1.DataBind()
            End If
        End If
    End Sub

    Protected Sub SearchResult_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles SearchResult.ItemCommand
        If (e.CommandName = "selectBusiness") Then

            Dim businessData = dbmanager.getBusinessDetailsBID(Convert.ToInt32(e.CommandArgument))
            tbxBusinessName.Text = businessData(0)
            ddlCategory.SelectedItem.Text = businessData(1)
            tbxShopURL.Text = businessData(2)
            tbxShopDetails.Text = businessData(3)
            businessID = e.CommandArgument
            Dim outletData = dbmanager.getBusinessDetailsBID(Convert.ToInt32(e.CommandArgument))
            hfBusinessID.Value = e.CommandArgument
            GridView1.DataBind()

            tbxShopOutletName.Enabled = True
            tbxOutletAddress.Enabled = True
            tbxShopOutletTel.Enabled = True
            tbxOpeningHours.Enabled = True
            tbxRemarks.Enabled = True


        End If
    End Sub
    'uploading image
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
        If (fileuploadImage.HasFile) Then
            Dim imgLength As Integer = fileuploadImage.PostedFile.ContentLength
            Dim img As HttpPostedFile = fileuploadImage.PostedFile
            Dim imgName As String = Path.GetFileName(fileuploadImage.PostedFile.FileName)

            hfImageID.Value = dbmanager.addImage(imgName, img, imgLength).ToString()

        End If
    End Sub

    Protected Sub BtnAddMoreOutlets_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnAddMoreOutlets.Click


        tbxShopOutletName.Enabled = True
        tbxOutletAddress.Enabled = True
        tbxShopOutletTel.Enabled = True
        tbxOpeningHours.Enabled = True
        tbxRemarks.Enabled = True

        Dim datecreated As Date
        datecreated = Today.Date

        If (dbmanager.addOutletDetails(businessID, tbxShopOutletName.Text, tbxOutletAddress.Text, tbxOpeningHours.Text, tbxRemarks.Text, 0, tbxShopOutletTel.Text, datecreated)
            ) Then
            ' clear all the textboxes  & disable the textboxes
            tbxShopOutletName.Text = ""
            tbxOutletAddress.Text = ""
            tbxShopOutletTel.Text = ""
            tbxOpeningHours.Text = ""
            tbxRemarks.Text = ""
            MsgBox("Successfully added outlet", MsgBoxStyle.OkOnly, "Success")
            'refresh of gridview
            GridView1.DataBind()
        End If
    End Sub

End Class